package com.epfly.promo.activity.impl.discount.predicate;

import com.epfly.iface.promo.activity.enums.EffectiveOrderMethodsEnum;
import com.epfly.iface.promo.activity.enums.EffectiveTakeMethodsEnum;
import com.epfly.promo.activity.entity.ActivityEntity;
import com.epfly.promo.activity.impl.ActivityUtil;
import com.epfly.promo.activity.impl.discount.ActivityWrapper;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

/**
 * 取餐方式校验
 */
@Order(value = PredicateOrder.TAKE_METHOD_ORDER)
@Service
public class TakeMethodPredicate implements ActivityDiscountPredicate {


    @Override
    public boolean test(ActivityWrapper activityWrapper) {

        ActivityEntity activityEntity = activityWrapper.getActivityEntity();

        Byte orderMethod = activityWrapper.getOrderMethod();
        Byte takeMethod = activityWrapper.getTakeMethod();

        // 下单方式选择了"预定"
        if (orderMethod.equals(EffectiveOrderMethodsEnum.PRE.getKey())) {
            // 取餐方式不限
            if (activityEntity.getEffectiveTakeMethods().equals(ActivityUtil.join(EffectiveTakeMethodsEnum.NO_LIMIT.getKey()))) {
                return true;
            }
            // 优惠活动设置的"取餐方式"包含当前订单使用的"取餐方式"
            return activityEntity.getEffectiveTakeMethods().contains(ActivityUtil.join(takeMethod));
        }

        return true;
    }
}
